<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <!--<div class="filter-container">
      <el-select v-model="listQuery.status" clearable style="width: 200px" class="filter-item" placeholder="请选择状态" >
        <el-option v-for="(item,index) in statusMap" :key="index" :label="item.name" :value="item.value" />
      </el-select>
      <el-button
        v-permission="['promote:accountsreceivable:query']"
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >查找</el-button>
    </div>-->

    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      size="small"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
      style="white-space: pre-line"
    >
      <el-table-column prop="gmtCreate" align="center" label="单据日期">
        <template slot-scope="scope">{{ scope.row.addTime | formatGmt }}</template>
      </el-table-column>
      <el-table-column prop="warehouseOrderNo" align="center" label="单据编号" >
        <template slot-scope="scope">
          <router-link :to="{ path:'/warehouse/suppdetail?id='+scope.row.id + '&type='+ customerType}">
            <span style="color: green">{{ scope.row.warehouseOrderNo }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column prop="paymentAllAmount" align="center" label="总金额"/>
      <el-table-column prop="paymentYouhuiAmount" align="center" label="优惠金额"/>
      <el-table-column prop="paymentAmount" align="center" label="应结算额"/>
      <el-table-column prop="finishAmount" align="center" label="已结算额"/>
      <el-table-column prop="finishAmount" align="center" label="未结算额">
        <template slot-scope="scope">{{ scope.row.paymentAmount - scope.row.finishAmount }}</template>
      </el-table-column>
      <el-table-column prop="status" align="center" label="状态">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.status == 1 ? 'success' : 'info' "
          >{{ scope.row.status == 1 ? '结算中' : '结算完成' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="adminName" align="center" label="操作人"/>
      <el-table-column align="center" label="操作" width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-permission="['generator:accountsreceivable:update']"
            v-if="scope.row.status ==1"
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row)"
          >结算</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNo"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="dataForm"
        status-icon
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="应付金额" prop="paymentAmount">
          {{ dataForm.paymentAmount }}
        </el-form-item>
        <el-form-item label="已付金额" prop="finishAmount">
          {{ dataForm.finishAmount }}
        </el-form-item>
        <el-form-item label="未结算金额" prop="finishAmount">
          {{ dataForm.paymentAmount - dataForm.finishAmount }}
        </el-form-item>
        <el-form-item label="结算金额" prop="applyAmount">
          <el-input v-model="dataForm.applyAmountF" clearable placeholder="">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="对接人" prop="customerName">
          <el-input v-model="dataForm.customerName" clearable placeholder=""/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="applyData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
.el-table .cell {
  white-space: pre-line;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 120px;
  height: 120px;
  display: block;
}
</style>

<script>
import { list, update } from '@/api/financepayable'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
export default {
  name: 'FinancePayAble',
  components: { Pagination },
  filters: {
  },
  data() {
    return {
      statusMap: [{ value: 0, name: '禁用' }, { value: 1, name: '正常' }],
      list: undefined,
      total: 0,
      listLoading: true,
      supplierId: this.$route.query.supplierId,
      customerType: this.$route.query.type,
      listQuery: {
        pageNo: 1,
        limit: 20,
        supplierId: this.$route.query.supplierId,
        customerType: this.$route.query.type
      },
      options: [],
      dataForm: {
        id: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      list(this.listQuery)
        .then(response => {
          response.data.data.items.forEach(item => {
            item.paymentAllAmount = item.paymentAllAmount / 100
            item.paymentYouhuiAmount = item.paymentYouhuiAmount / 100
            item.paymentAmount = item.paymentAmount / 100
            item.finishAmount = item.finishAmount / 100
          })
          this.list = response.data.data.items
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        id: undefined
      }
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.$set(this.dataForm, 'applyAmountF', row.paymentAmount - row.finishAmount)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    applyData() {
      if (this.dataForm.applyAmountF == null) {
        this.$notify.error({
          title: '失败',
          message: '结算金额不能为空'
        })
        return
      }
      if (this.dataForm.applyAmountF > (this.dataForm.paymentAmount - this.dataForm.finishAmount)) {
        this.$notify.error({
          title: '失败',
          message: '结算金额不能大于未结算金额'
        })
        return
      }
      this.dataForm.applyAmount = parseInt(this.dataForm.applyAmountF * 100)
      update(this.dataForm)
        .then(response => {
          this.getList()
          this.dialogFormVisible = false
          this.$notify.success({
            title: '成功',
            message: '更新成功'
          })
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: '失败'
          })
        })
    }
  }
}
</script>

